from app.db.mongoClient import ErrMongoClient, get_mongo_client
from . import dbSynPosUser, collectionUserList
from app.db.dbDiningQuery import queryDiningName, queryAllDiningNo, queryDiningInfoByNo
from app.db.dbStallQuery import queryStallName 
from app.db.dbUserRole import getRoleNameById
from app.settings.base import super_administrator
from app.globals.publicMethod import globalTimeToString

def queryAllUser():
    '''
    查询所有用户
    '''
    try:
        mg_client = get_mongo_client()
        res = mg_client[dbSynPosUser][collectionUserList].find()
        userList = []
        for v in res:
            # 根据角色Id获取角色名
            userRoleId = v.get('userRoleId')
            userRoleName = getRoleNameById(userRoleId)
            # 获取用户关联的餐厅号、餐厅名
            RelationDiningNoList = []
            DiningNoList = v.get('RelationDiningNoList')
            if userRoleName == super_administrator:
                # 不返回超级管理员的信息
                continue
            
            if DiningNoList is not None:
                for id in DiningNoList:
                    dininginfo = {
                        'diningNo': id,
                        'diningName': queryDiningName(id) # 根据餐厅号获取餐厅名
                    }
                    RelationDiningNoList.append(dininginfo)

            # 获取关联档口号和档口名
            RelationStallNo = ''
            temp = v.get('RelationStallNo')
            if temp is not None:
                stallinfo = {
                    'stallNo': temp,
                    'stallName': queryStallName(temp) # 根据档口号获取档口名
                }
                RelationStallNo = stallinfo
                
            RelationDiningNoStallNoList = []
            if DiningNoList is not None:
                for dining_no in DiningNoList:
                    diningName, stallNos = queryDiningInfoByNo(dining_no)
                    dininginfo = {
                        'diningNo': dining_no,
                        'diningName': diningName,
                        'stallNo': stallNos
                    }
                    RelationDiningNoStallNoList.append(dininginfo)
            
            users = {
                'userId': v.get('userId'),
                'userName': v.get('userName'),
                'userRealName': v.get('userRealName'),
                'userDesc': v.get('userDesc'),
                'userRoleId': userRoleId,
                'userRoleName': userRoleName,
                'userState': v.get('userState'),
                'createTime': globalTimeToString(v.get('createTime')),
                'updateTime': globalTimeToString(v.get('updateTime')),
                'creatorUserName': v.get('creatorUserName'),
                'menderUserName': v.get('menderUserName'),
                'RelationDiningNoList': RelationDiningNoList,
                'RelationStallNo': RelationStallNo,
                'RelationDiningNoStallNoList': RelationDiningNoStallNoList
            }
            userList.append(users)
        return userList
    except Exception as e:
        raise ErrMongoClient.data(str(e))


def queryUserDiningStall(userName):
    '''
    查询用户关联的餐厅档口 
    '''
    try:
        mg_client = get_mongo_client()
        cond = {'userName': userName}
        collection = mg_client[dbSynPosUser][collectionUserList]
        res = collection.find_one(cond)
        if res is None:
           return None

        userRoleId = res.get('userRoleId')
        userRoleName = getRoleNameById(userRoleId)

        relationDiningNoList = []
        if userRoleName == super_administrator:
            # 超级管理员获取所有的餐厅号
            relationDiningNoList = queryAllDiningNo()
        else:
            relationDiningNoList = res.get('RelationDiningNoList')
            
        res_relationDiningNoList = []
        if relationDiningNoList is not None:
            for id in relationDiningNoList:
                dininginfo = {
                    'diningNo': id,
                    'diningName': queryDiningName(id) # 根据餐厅号获取餐厅名
                }
                res_relationDiningNoList.append(dininginfo)
                            
        RelationDiningNoStallNoList = []
        for ids in res_relationDiningNoList:
            diningName, stallNos = queryDiningInfoByNo(ids.get('diningNo'))
            dininginfo = {
                'diningNo': id,
                'diningName': diningName,
                'stallNo': stallNos
            }
            RelationDiningNoStallNoList.append(dininginfo)

        info = {
            'RelationDiningNoStallNoList': RelationDiningNoStallNoList
        }

        return info
    except Exception as e:
        raise ErrMongoClient.data(str(e))